let authTool  = require('../tools/auth-tool');


function assembleParams(keyValueArray){
  let str  = '';

  for(let param of keyValueArray){
    str  += param.key;
    str  += '=';
    str  += param.value;
    str  += '&';
  }

  return str;
}


function buildUrl(path, params){
  let url  = path;

  if(params && params.size){
    url  = path + '?';
    url  += assembleParams(params);
  }

  return url;
}


function buildOptions(method, json){
  let options  = {
    method:method
  };

  if(json){
    options.headers  = {"Content-Type":"application/json"};
    options.body  = JSON.stringify(json);
  }

  return options;
}


export async function request(url, options){
  try{
    const response  = await fetch(url, options);

    try{
      const json  = await response.json();
      console.log('json', json);
      if(response.status == 200){
        return {status:response.status, data:json};
      }
      else{
        if(json.message){
          return {status:response.status, data:json.message};
        }
        else if(json.msg){
          return {status:response.status, data:json.msg};
        }else{
          return {status:response.status, data:'Unknown'};
        }
      }
    }catch(jsonE){
      console.error("JSON Exception", jsonE);
      return {status:100, data:'Jons error'};
    }
  }catch(e){
    console.error('REQUEST Exception', e);
    return {status:0, data:e};
  }
}


export async function requestWithAuth(url, options){
  let optionsWithAuth  = options;

  if(optionsWithAuth === undefined){
    optionsWithAuth  = {};
  }

  if(optionsWithAuth.headers === undefined){
    optionsWithAuth.headers  = {'Content-Type':'application/json'};
  }

  if(optionsWithAuth.Authorization === undefined){
    optionsWithAuth.headers.Authorization  = authTool.getToken();
  }

  return await request(url, optionsWithAuth);
}


export async function get(path, params){
  const url  = buildUrl(path, params);
  return await request(url);
}


export async function getWithAuth(path, params){
  const url  = buildUrl(path, params);
  return await requestWithAuth(url);
}


export async function post(path, params, jsonData){
  const url  = buildUrl(path, params);
  const options  = buildOptions('POST', jsonData);
  return await request(url, options);
}


export async function postWithAuth(path, params, jsonData){
  const url  = buildUrl(path, params);
  const options  = buildOptions('POST', jsonData);
  return await requestWithAuth(url, options);
}


export async function put(path, params, jsonData){
  const url  = buildUrl(path, params);
  const options  = buildUrl('PUT', jsonData);
  return await request(url, options);
}


export async function putWithAuth(path, params, jsonData){
  const url  = buildUrl(path, params);
  const options  = buildUrl('PUT', jsonData);
  return await requestWithAuth(url, options);
}


export async function del(path, params, jsonData){
  const url  = buildUrl(path, params);
  const options  = buildOptions('DELETE', jsonData);
  return await request(url, options);
}


export async function delWithAuth(path, params, jsonData){
  const url  = buildUrl(path, params);
  const options  = buildOptions('DELETE', jsonData);
  return await request(url, options);
}


export async function patch(path, params, jsonData){
  const url  = buildUrl(path, params);
  const options  = buildOptions('PATCH', jsonData);
  return await request(url, options);
}


export async function pathchWithAuth(path, params, jsonData){
  const url  = buildUrl(path, params);
  const options  = buildOptions('PATCH', jsonData);
  return await requestWithAuth(url, options);
}
